#pragma once
#include <memory>
#include <vector>
#include "hwdrv/com_drv.h"
#include "hwdrv/lan_drv.h"
namespace hwdrv {
class HwMgr
{
public:
    HwMgr();
    static HwMgr& get();

    /**
     * @brief 初始化硬件管理器
     * @param cabtype 机柜类型
     * @param is_vm 是否为虚拟机,-1表示根据配置文件判断，0表示不是虚拟机，1表示是虚拟机
     */
    int init(uint8_t cabtype, int is_vm = -1);

    struct BusInfo
    {
        std::string type;  //总线类型,如jkcan，ecat，canopen

        /**
         * @brief 总线参数
         * @details 对于jkcan，interface为c_0_1,c_0_2,s_0，s_1,对应于controlcan的dev0-canid0，dev0-canid1，
         * @details 对于ecat，interface为3，4，5，对应于LAN3，LAN4，LAN5
         * @details 对于canopen，interface为0，1，对应于socketcan的can0，can1
         */
        std::string interface;
        std::string name;  //用于rtbus索引的名字，一般为%type%id，如jkcan0，ecat0
    };
    struct RobSlotInfo
    {
        int slot_index;
        int devid;
        BusInfo bus;
    };
    struct ScbSlotInfo
    {
        int type;     //SCB类型
        int devid;    //SCB设备在总线上的ID
        BusInfo bus;  //总线参数
    };
    inline RobSlotInfo* get_rob_slot(uint8_t slot_index)
    {
        if (slot_index < rob_slots_.size())
            return &rob_slots_[slot_index];
        return nullptr;
    }
    inline size_t get_rob_slot_cnt() { return rob_slots_.size(); }
    inline const ScbSlotInfo& get_scb_slot() { return scb_slot_; }

    std::shared_ptr<ComPort> get_com(uint8_t com_id)
    {
        if (com_id < com_ports_.size())
            return com_ports_[com_id];
        return nullptr;
    }
    std::shared_ptr<LanPort> get_lan(uint8_t lan_id)
    {
        if ((lan_id - 1) < (uint8_t)lan_ports_.size())
            return lan_ports_[lan_id - 1];
        return nullptr;
    }
    bool is_sim() const { return is_sim_; }

private:
    int load_bus(void*, int id, BusInfo& bus);
    int load_scb(void*);
    int load_rob(void*);
    int load_lan(void*);
    int load_com(void*);

private:
    std::vector<std::shared_ptr<ComPort>> com_ports_;
    std::vector<std::shared_ptr<LanPort>> lan_ports_;
    std::vector<RobSlotInfo> rob_slots_;
    ScbSlotInfo scb_slot_;

    std::string cfg_file_;
    std::string name_;
    bool is_sim_;
    uint8_t type_;

    // friend class LanPort;
};
}  // namespace hwdrv
